local utils = require "astronvim.utils"
return {
  { "microsoft/python-type-stubs", lazy = true },
  { "pandas-dev/pandas-stubs", lazy = true },
  {
    "nvim-treesitter/nvim-treesitter",
    opts = function(_, opts)
      if opts.ensure_installed ~= "all" then
        opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "python", "toml" })
      end
    end,
  },
  {
    "williamboman/mason-lspconfig.nvim",
    opts = function(_, opts)
      opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, {
        -- "pyright",
        -- "ruff_lsp",
      })
    end,
  },
  {
    "jay-babu/mason-null-ls.nvim",
    opts = function(_, opts) opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, {}) end,
  },
  -- {
  --   "jay-babu/mason-nvim-dap.nvim",
  --   opts = function(_, opts)
  --     opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, "python")
  --     if not opts.handlers then opts.handlers = {} end
  --     opts.handlers.python = function() end -- make sure python doesn't get set up by mason-nvim-dap, it's being set up by nvim-dap-python
  --   end,
  -- },
  {
    "linux-cultist/venv-selector.nvim",
    keys = {
      { "<leader>lv", "<cmd>VenvSelect<cr>", desc = "Select VirtualEnv" },
      { "<leader>lV", "<cmd>VenvSelectCurrent<cr>", desc = "Show Current VirtualEnv" },
    },
    ft = "python",
    opts = function(_, opts)
      vim.api.nvim_create_autocmd("FileType", {
        desc = "Auto select virtualenv Nvim open",
        pattern = "python",
        callback = function()
          local util = require "lspconfig/util"
          local root_files = {
            ".git",
            "pyproject.toml",
            "setup.py",
            "setup.cfg",
            "requirements.txt",
            "Pipfile",
            "pyrightconfig.json",
            "main.py",
            ".neoconf.json",
          }
          local venv = util.root_pattern(unpack(root_files))
          if venv ~= "" then require("venv-selector").retrieve_from_cache() end
        end,
        once = true,
      })
      if vim.fn.executable "conda" == 1 then
        local cmd_out = vim.fn.system "conda info --base"
        local base_path = string.gsub(cmd_out, "\n", "")
        local env_path = base_path .. "/envs"
        opts.anaconda_base_path = base_path
        opts.anaconda_envs_path = env_path
      end
      opts.name = { "venv", ".venv" }
      return opts
    end,
  },
  -- {
  --   "mfussenegger/nvim-dap-python",
  --   dependencies = "mfussenegger/nvim-dap",
  --   ft = "python", -- NOTE: ft: lazy-load on filetype
  --   config = function(_, opts)
  --     local path = require("mason-registry").get_package("debugpy"):get_install_path() .. "/venv/bin/python"
  --     require("dap-python").setup(path, opts)
  --   end,
  -- },
}
